﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using CodeAngel.UI.Extensions;

namespace CodeAngel.UI
{
    [ContentProperty("ItemContent")]
    public partial class ListItemControl : UserControl
    {
        #region Constructors
        
        public ListItemControl()
        {
            InitializeComponent();
        }

        #endregion

        #region ItemContent

        /// <summary>
        /// Gets or sets the ItemContent.
        /// </summary>
        public UIElement ItemContent
        {
            get { return (UIElement)GetValue(ItemContentProperty); }
            set { SetValue(ItemContentProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for ItemContent.
        /// </summary>
        public static readonly DependencyProperty ItemContentProperty =
            DependencyProperty.Register("ItemContent", typeof(UIElement), typeof(ListItemControl), new PropertyMetadata(null, ItemContentChangedHandler));

        /// <summary>
        /// Handles when the ItemContent changes.
        /// </summary>
        private static void ItemContentChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ThisControl = d as ListItemControl;
            if (ThisControl != null)
            {
                ThisControl.ContentPlaceholder.Content = ThisControl.ItemContent;
            }

        }

        #endregion

		#region Click

		/// <summary>
		/// Provides notification that the Clic has k.
		/// </summary>
		public event EventHandler Click;

		/// <summary>
		/// Raises the Click event.
		/// </summary>
        protected void RaiseClickEvent(object sender, RoutedEventArgs e)
		{
		    if (this.Click != null) this.Click(this.DataContext, EventArgs.Empty);
            VisualStateManager.GoToState(this, "Normal", true);


            if (this.OpenCommand != null) this.OpenCommand.Execute(this.DataContext);

		}

		#endregion

        #region Delete

        /// <summary>
        /// Provides notification that the Clic has k.
        /// </summary>
        public event EventHandler Delete;

        /// <summary>
        /// Raises the Delete event.
        /// </summary>
        protected void RaiseDeleteEvent(object sender, RoutedEventArgs e)
        {
            if (this.Delete != null) this.Delete(this.DataContext, EventArgs.Empty);
            VisualStateManager.GoToState(this, "Normal", true);

            if (this.DeleteCommand != null) this.DeleteCommand.Execute(this.DataContext);
        }

        #endregion

        #region MarkedAsDeleted

        /// <summary>
        /// Gets or sets the MarkedAsDeleted.
        /// </summary>
        public bool MarkedAsDeleted
        {
            get { return (bool)GetValue(MarkedAsDeletedProperty); }
            set { SetValue(MarkedAsDeletedProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for MarkedAsDeleted.
        /// </summary>
        public static readonly DependencyProperty MarkedAsDeletedProperty =
            DependencyProperty.Register("MarkedAsDeleted", typeof(bool), typeof(ListItemControl), new PropertyMetadata(false, MarkedAsDeletedChangedHandler));

        /// <summary>
        /// Handles when the MarkedAsDeleted changes.
        /// </summary>
        private static void MarkedAsDeletedChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ThisControl = d as ListItemControl;
            if (ThisControl != null)
            {
                if (ThisControl.MarkedAsDeleted)
                    ThisControl.MarkedAsDeletedPanel.AnimateOpacity(1);
                else
                    ThisControl.MarkedAsDeletedPanel.AnimateOpacity(0, Visibility.Collapsed);
            }

        }
        
        private void UndoMarkedAsDeleted(object sender, RoutedEventArgs e)
        {
            this.MarkedAsDeleted = false;
        }

        #endregion

        #region OpenCommand

        /// <summary>
        /// Gets or sets the OpenCommand.
        /// </summary>
        public ICommand OpenCommand
        {
            get { return (ICommand)GetValue(OpenCommandProperty); }
            set { SetValue(OpenCommandProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for OpenCommand.
        /// </summary>
        public static readonly DependencyProperty OpenCommandProperty =
            DependencyProperty.Register("OpenCommand", typeof(ICommand), typeof(ListItemControl), new PropertyMetadata(null));

        #endregion

        #region DeleteCommand

        /// <summary>
        /// Gets or sets the DeleteCommand.
        /// </summary>
        public ICommand DeleteCommand
        {
            get { return (ICommand)GetValue(DeleteCommandProperty); }
            set { SetValue(DeleteCommandProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for DeleteCommand.
        /// </summary>
        public static readonly DependencyProperty DeleteCommandProperty =
            DependencyProperty.Register("DeleteCommand", typeof(ICommand), typeof(ListItemControl), new PropertyMetadata(null));

        #endregion

        #region ShowDeleteCommand

        /// <summary>
        /// Gets or sets the ShowDeleteCommand.
        /// </summary>
        public bool ShowDeleteCommand
        {
            get { return (bool)GetValue(ShowDeleteCommandProperty); }
            set { SetValue(ShowDeleteCommandProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for ShowDeleteCommand.
        /// </summary>
        public static readonly DependencyProperty ShowDeleteCommandProperty =
            DependencyProperty.Register("ShowDeleteCommand", typeof(bool), typeof(ListItemControl), new PropertyMetadata(true, ShowDeleteCommandChangedHandler));

        /// <summary>
        /// Handles when the ShowDeleteCommand changes.
        /// </summary>
        private static void ShowDeleteCommandChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ThisControl = d as ListItemControl;
            if (ThisControl != null)
            {
                ThisControl.DeleteButton.Visibility = ThisControl.ShowDeleteCommand ? Visibility.Visible : Visibility.Collapsed;
            }

        }

        #endregion

        #region IsVisible

        /// <summary>
        /// Gets or sets the IsVisible.
        /// </summary>
        public bool IsVisible
        {
            get { return (bool)GetValue(IsVisibleProperty); }
            set { SetValue(IsVisibleProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for IsVisible.
        /// </summary>
        public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.Register("IsVisible", typeof(bool), typeof(ListItemControl), new PropertyMetadata(true, IsVisibleChangedHandler));

        /// <summary>
        /// Handles when the IsVisible changes.
        /// </summary>
        private static void IsVisibleChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ThisControl = d as ListItemControl;
            if (ThisControl != null)
            {
                //if (ThisControl.OriginalHeight > 0) ThisControl.OriginalHeight = ThisControl.ActualHeight;
                if (ThisControl.IsVisible)
                {
                    ThisControl.Visibility = Visibility.Visible;
                    ThisControl.AnimateHeight(ThisControl.TargetHeight);
                }
                else
                {
                    ThisControl.AnimateHeight(0, delegate
                    {
                        ThisControl.Visibility = Visibility.Collapsed;
                    });
                }

            }

        }

        //private double OriginalHeight = -1.0;

        #endregion

        public double TargetHeight { get; set; }
        
    }
}
